using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

using SBPweb.Modules.Modularity;
using SBPweb.Contracts;
using SBPweb.Contracts.Implementation;

namespace SBPweb.Modules.ChatController
{
	public class ChatConfiguration
	{
		#region Private properties

		private bool mPlayBackgroundSound = false;
		private bool mShowOfflinesForConference = false;
		private bool mShowHistory = true;
		private int mHistoryLines = 15;
		private Font mOutGoingMessageFont = new Font(FontFamily.GenericSerif, 9);
		private Font mIncomingMessageFont = new Font(FontFamily.GenericSerif, 9);
		private Font mOtherTextFont = new Font(FontFamily.GenericSerif, 9);
		private Font mOwnerMessageFont = new Font(FontFamily.GenericSerif, 9);
		private Font mOwnerOtherTextFont = new Font(FontFamily.GenericSerif, 9);
		private Font mChatHistoryTextFont = new Font(FontFamily.GenericSerif, 9);
		private Font mChatHistoryOtherTextFont = new Font(FontFamily.GenericSerif, 9);
		private Color mOutGoingMessageColor = Color.Black;
		private Color mInComingMessageColor = Color.Black;
		private Color mOtherTextColor = Color.Black;
		private Color mOwnerMessageColor = Color.Black;
		private Color mOwnerOtherTextColor = Color.Black;
		private Color mChatHistoryTextColor = Color.Black;
		private Color mChatHistoryOtherTextColor = Color.Black;
		private IModule mParentModule;

		#endregion

		#region Public properties

		public Font OutGoingMessageFont
		{
			get { return mOutGoingMessageFont; }
			set { mOutGoingMessageFont = value; }
		}

		public Font IncomingMessageFont
		{
			get { return mIncomingMessageFont; }
			set { mIncomingMessageFont = value; }
		}

		public Font OtherTextFont
		{
			get { return mOtherTextFont; }
			set { mOtherTextFont = value; }
		}

		public Font OwnerMessageFont
		{
			get { return mOwnerMessageFont; }
			set { mOwnerMessageFont = value; }
		}

		public Font OwnerOtherTextFont
		{
			get { return mOwnerOtherTextFont; }
			set { mOwnerOtherTextFont = value; }
		}

		public Font ChatHistoryTextFont
		{
			get { return mChatHistoryTextFont; }
			set { mChatHistoryTextFont = value; }
		}

		public Font ChatHistoryOtherTextFont
		{
			get { return mChatHistoryOtherTextFont; }
			set { mChatHistoryOtherTextFont = value; }
		}

		public Color OutGoingMessageColor
		{
			get { return mOutGoingMessageColor; }
			set { mOutGoingMessageColor = value; }
		}

		public Color InComingMessageColor
		{
			get { return mInComingMessageColor; }
			set { mInComingMessageColor = value; }
		}

		public Color OtherTextColor
		{
			get { return mOtherTextColor; }
			set { mOtherTextColor = value; }
		}

		public Color OwnerMessageColor
		{
			get { return mOwnerMessageColor; }
			set { mOwnerMessageColor = value; }
		}

		public Color OwnerOtherTextColor
		{
			get { return mOwnerOtherTextColor; }
			set { mOwnerOtherTextColor = value; }
		}

		public Color ChatHistoryTextColor
		{
			get { return mChatHistoryTextColor; }
			set { mChatHistoryTextColor = value; }
		}

		public Color ChatHistoryOtherTextColor
		{
			get { return mChatHistoryOtherTextColor; }
			set { mChatHistoryOtherTextColor = value; }
		}

		public bool PlayBackgroundSound
		{
			get { return mPlayBackgroundSound; }
			set { mPlayBackgroundSound = value; }
		}

		public bool ShowOfflinesForConference
		{
			get { return mShowOfflinesForConference; }
			set { mShowOfflinesForConference = value; }
		}

		public bool ShowHistory
		{
			get { return mShowHistory; }
			set { mShowHistory = value; }
		}

		public int HistoryLines
		{
			get { return mHistoryLines; }
			set { mHistoryLines = value; }
		}

		#endregion

		#region Constructor logic

		public ChatConfiguration(IModule parent)
		{
			mParentModule = parent;
		}

		public ChatConfiguration(IModule parent, PersistencyDictionary state)
			: this(parent)
		{
			mShowOfflinesForConference = state.GetBoolean("ShowOfflines", mShowOfflinesForConference);
			mPlayBackgroundSound = state.GetBoolean("PlayBackground", mPlayBackgroundSound);
			mShowHistory = state.GetBoolean("ShowHistory", mShowHistory);

			mHistoryLines = state.GetInt("HistoryLines", mHistoryLines);

			mOutGoingMessageColor = Color.FromArgb(state.GetInt("OutGoingMessageColor", 0));
			mInComingMessageColor = Color.FromArgb(state.GetInt("InComingMessageColor", 0));
			mOtherTextColor = Color.FromArgb(state.GetInt("OtherTextColor", 0));
			mOwnerMessageColor = Color.FromArgb(state.GetInt("OwnerMessageColor", 0));
			mOwnerOtherTextColor = Color.FromArgb(state.GetInt("OwnerOtherTextColor", 0));
			mChatHistoryTextColor = Color.FromArgb(state.GetInt("ChatHistoryTextColor", 0));
			mChatHistoryOtherTextColor = Color.FromArgb(state.GetInt("ChatHistoryOtherTextColor", 0));

			LoadFont(state, "OutGoingMessageFont", out mOutGoingMessageFont);
			LoadFont(state, "IncomingMessageFont", out mIncomingMessageFont);
			LoadFont(state, "OtherTextFont", out mOtherTextFont);
			LoadFont(state, "OwnerMessageFont", out mOwnerMessageFont);
			LoadFont(state, "OwnerOtherTextFont", out mOwnerOtherTextFont);
			LoadFont(state, "ChatHistoryTextFont", out mChatHistoryTextFont);
			LoadFont(state, "ChatHistoryOtherTextFont", out mChatHistoryOtherTextFont);

			mParentModule.Host.LogManager.WriteVerbose(this, "Chat configuration loaded from registry.", "ChatController");
		}

		#endregion

		#region Persistency

		public void SavePersistence(PersistencyDictionary state)
		{
			state.SetBoolean("ShowOfflines", mShowOfflinesForConference);
			state.SetBoolean("PlayBackground", mPlayBackgroundSound);
			state.SetBoolean("ShowHistory", mShowHistory);

			state.SetInt("HistoryLines", mHistoryLines);

			state.SetInt("OutGoingMessageColor", mOutGoingMessageColor.ToArgb());
			state.SetInt("InComingMessageColor", mInComingMessageColor.ToArgb());
			state.SetInt("OtherTextColor", mOtherTextColor.ToArgb());
			state.SetInt("OwnerMessageColor", mOwnerMessageColor.ToArgb());
			state.SetInt("OwnerOtherTextColor", mOwnerOtherTextColor.ToArgb());
			state.SetInt("ChatHistoryTextColor", mChatHistoryTextColor.ToArgb());
			state.SetInt("ChatHistoryOtherTextColor", mChatHistoryOtherTextColor.ToArgb());

			SaveFont(state, "OutGoingMessageFont", mOutGoingMessageFont);
			SaveFont(state, "IncomingMessageFont", mIncomingMessageFont);
			SaveFont(state, "OtherTextFont", mOtherTextFont);
			SaveFont(state, "OwnerMessageFont", mOwnerMessageFont);
			SaveFont(state, "OwnerOtherTextFont", mOwnerOtherTextFont);
			SaveFont(state, "ChatHistoryTextFont", mChatHistoryTextFont);
			SaveFont(state, "ChatHistoryOtherTextFont", mChatHistoryOtherTextFont);

			mParentModule.Host.LogManager.WriteVerbose(this, "Chat configuration saved to registry.", "ChatController");
		}

		#endregion

		#region Private helper methods

		private void LoadFont(PersistencyDictionary state, string key, out Font font)
		{
			string fontFamily = state.GetString(key, "Arial");
			float fontSize = 10f;
			string fontStyle = state.GetString(string.Format("{0}_Style", key), "Regular");
			try
			{
				fontSize = Convert.ToSingle(state.GetInt(string.Format("{0}_Size", key), 10));
			}
			catch { }

			FontStyle fs = new FontStyle();
			fs = FontStyle.Regular;
			if (fontStyle.Contains(FontStyle.Bold.ToString()))
			{
				fs = fs | FontStyle.Bold;
			}
			if (fontStyle.Contains(FontStyle.Italic.ToString()))
			{
				fs = fs | FontStyle.Italic;
			}
			if (fontStyle.Contains(FontStyle.Strikeout.ToString()))
			{
				fs = fs | FontStyle.Strikeout;
			}
			if (fontStyle.Contains(FontStyle.Underline.ToString()))
			{
				fs = fs | FontStyle.Underline;
			}
			font = new Font(fontFamily, fontSize, fs);
		}

		private void SaveFont(PersistencyDictionary state, string key, Font font)
		{
			if (font == null)
			{
				return;
			}
			state.SetString(key, font.FontFamily.Name);
			state.SetString(string.Format("{0}_Style", key), font.Style.ToString());
			state.SetInt(string.Format("{0}_Size", key), (int)Math.Round(font.Size));
		}

		#endregion
	}
}
